﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ClassModel;
using System.Data.SqlClient;
using System.Data;

namespace ClassDAL
{
    public class t_ClassTeacherCourse_NoSchedule_DAL
    {

        /// <summary>
        /// 插入不排课数据
        /// </summary>
        /// <param name="noSchedule"></param>
        public static int InsertNoScheduleData(t_ClassTeacherCourse_NoSchedule noSchedule)
        {
            // 首先要删除之前的记录

            DeleteNoSchedule(noSchedule);

            string insertSQL = "insert into t_ClassTeacherCourse_noSchedule values(@dataid,@taskid,@data,@type,@createtime);select @@IDENTITY;";

            SqlParameter[] insertParams = new SqlParameter[] 
            {
                new SqlParameter("@dataid",noSchedule.dataid),
                new SqlParameter("@taskid",noSchedule.taskid),
                new SqlParameter("@data",noSchedule.data),
                new SqlParameter("@type",noSchedule.type),
                new SqlParameter("@createtime",noSchedule.createtime)
            };


            return Convert.ToInt32(SQLHelper.ExecuteScalar(SQLHelper.GetConnection(), System.Data.CommandType.Text, insertSQL, insertParams));

        }

        /// <summary>
        /// 删除制定任务
        /// </summary>
        /// <param name="noSchedule"></param>
        /// <returns></returns>
        public static int DeleteNoSchedule(t_ClassTeacherCourse_NoSchedule noSchedule)
        {
            string deleteSQL = "delete from t_ClassTeacherCourse_noSchedule where dataid = @dataid and type = @type;";

            SqlParameter[] deleteParams = new SqlParameter[] 
            {
                new SqlParameter("@dataid",noSchedule.dataid),
                new SqlParameter("@type",noSchedule.type)
            };

            return SQLHelper.ExecuteNonQuery(SQLHelper.GetConnection(), System.Data.CommandType.Text, deleteSQL, deleteParams);
        }

        /// <summary>
        /// 获取老师、课程、班级的不排课记录
        /// </summary>
        /// <param name="taskid">任务id</param>
        /// <param name="type">class\teacher\course</param>
        /// <returns>null 表示，老师、课程、班级没有设置不排课。</returns>
        public static List<t_ClassTeacherCourse_NoSchedule> GetNoScheduleWithTaskid(int taskid, string type)
        {
            string selectSQL = "select id,dataid,taskid,data,type,createtime from t_ClassTeacherCourse_noSchedule where taskid = @taskid and type=@type;";

            SqlParameter[] selectParams = new SqlParameter[] 
            {
                new SqlParameter("@taskid",taskid),
                new SqlParameter("@type",type)
            };

            DataSet ds = SQLHelper.ExecuteDataset(SQLHelper.GetConnection(), CommandType.Text, selectSQL, selectParams);
            if (ds == null || ds.Tables.Count == 0) return null;

            List<t_ClassTeacherCourse_NoSchedule> resultList = new List<t_ClassTeacherCourse_NoSchedule>();
            DataRowCollection rows = ds.Tables[0].Rows;
            foreach (DataRow row in rows)
            {
                t_ClassTeacherCourse_NoSchedule noSchedule = new t_ClassTeacherCourse_NoSchedule();
                noSchedule.id = Convert.ToInt32(row["id"]);
                noSchedule.dataid = Convert.ToInt32(row["dataid"]);
                noSchedule.taskid = Convert.ToInt32(row["taskid"]);
                noSchedule.data = row["data"].ToString();
                noSchedule.type = row["type"].ToString();
                noSchedule.createtime = Convert.ToDateTime(row["createtime"]);

                resultList.Add(noSchedule);
            }

            return resultList;
        }

        /// <summary>
        /// 获得某个任务下的所有不排课数据(包括老师、班级、课程)
        /// </summary>
        /// <param name="taskid"></param>
        /// <returns></returns>
        public static List<t_ClassTeacherCourse_NoSchedule> GetTeacherClassCourseNoScheduleList(int taskid)
        {
            string selectSQL = "select id,dataid,taskid,data,type,createtime from t_ClassTeacherCourse_noSchedule where taskid = @taskid;";
            SqlParameter[] selectParams = new SqlParameter[] 
            {
                new SqlParameter("@taskid",taskid)
            };

            DataSet ds = SQLHelper.ExecuteDataset(SQLHelper.GetConnection(), CommandType.Text, selectSQL, selectParams);
            if (ds == null || ds.Tables.Count == 0) return null;

            List<t_ClassTeacherCourse_NoSchedule> ctcList = new List<t_ClassTeacherCourse_NoSchedule>();
            DataRowCollection rows = ds.Tables[0].Rows;
            foreach (DataRow row in rows)
            {
                t_ClassTeacherCourse_NoSchedule tcc = new t_ClassTeacherCourse_NoSchedule();
                tcc.id = Convert.ToInt32(row["id"]);
                tcc.dataid = Convert.ToInt32(row["dataid"]);
                tcc.taskid = Convert.ToInt32(row["taskid"]);
                tcc.data = row["data"].ToString();
                tcc.type = row["type"].ToString();
                tcc.createtime = Convert.ToDateTime(row["createtime"]);

                ctcList.Add(tcc);
            }

            return ctcList;
        }

    }
}
